package com.wertop.viid.controller;

import com.wertop.viid.common.annotation.LogOperation;
import com.wertop.viid.common.annotation.RequireAuth;
import com.wertop.viid.common.result.ConfirmStatusType;
import com.wertop.viid.common.result.ResponseStatusObject;
import com.wertop.viid.common.utils.LocalCacheUtil;
import com.wertop.viid.common.validator.ValidatorUtils;
import com.wertop.viid.dto.RegisterObject;
import com.wertop.viid.dto.UnRegisterObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 公共功能相关接口
 * @Author yempty
 * @Date 2020/6/2 10:32
 */
@RestController
@Api(tags = "公共功能相关接口")
public class CommonController {

    @RequireAuth
    @PostMapping("/System/Register")
    @ApiOperation("注册")
    @LogOperation("注册")
    public ResponseStatusObject register(@RequestBody RegisterObject registerObject) {
        // 检验参数
        ValidatorUtils.validateEntity(registerObject);
        // 添加至缓存
        LocalCacheUtil.put(registerObject.getDeviceId(), registerObject.getDeviceId());
        // 构造返回对象
        ResponseStatusObject responseStatusObject = ResponseStatusObject.create(ConfirmStatusType.OK);
        responseStatusObject.setId(registerObject.getDeviceId());
        return responseStatusObject;
    }

    @RequireAuth
    @PostMapping("/System/UnRegister")
    @ApiOperation("注销")
    public ResponseStatusObject unRegister(@RequestBody UnRegisterObject unRegisterObject) {
        ValidatorUtils.validateEntity(unRegisterObject);
        LocalCacheUtil.timedCache.remove(unRegisterObject.getDeviceId());
        return ResponseStatusObject.create(ConfirmStatusType.OK);
    }

    @RequireAuth
    @PostMapping("/System/Keepalive")
    @ApiOperation("保活")
    public ResponseStatusObject keepalive() {
        return ResponseStatusObject.create(ConfirmStatusType.OK);
    }
}
